package com.yunshang.ysmall.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yunshang.ysmall.model.IndexPicInfo;
import com.yunshang.ysmall.model.ProductInfo;
import com.yunshang.ysmall.model.ProductTypeInfo;
import com.yunshang.ysmall.service.IndexService;
import com.yunshang.ysmall.service.MemberService;
import com.yunshang.ysmall.service.ProductService;
import com.yunshang.ysmall.util.DateUtils;

import net.coobird.thumbnailator.Thumbnails;

@Controller
@RequestMapping("/index")
public class IndexController {
	
	@Resource
	IndexService indexService;
	@Resource
	MemberService memberService;
	
	public static final String ROOT = "static/upload-dir/";
	
	
	@RequestMapping("/savepic")
	public String savepic(@RequestParam("myfile") MultipartFile file, IndexPicInfo indexPicInfo,Model model){
		if(!file.isEmpty()){
			String filePath = upload(file);
			indexPicInfo.setImgPath(filePath);
		}
		if(indexService.insertIndexPic(indexPicInfo)){
			model.addAttribute("picList", indexService.getIndexPicList(null));
			return "/mng/main/index/piclist";
		}else{
			model.addAttribute("errmsg", "添加失败");
			return "/mng/main/index/addpic";
		}
	}
	
	private String upload(MultipartFile file){
		String desImgPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath()+ROOT;
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String fileName = UUID.randomUUID().toString()+suffix;
		try {
			Files.copy(file.getInputStream(), Paths.get(desImgPath,fileName));
//			Thumbnails.of(file.getInputStream()).size(400, 200).toFile(ROOT+"/min/"+fileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		return fileName;
	}
	
	@RequestMapping("/toaddpic")
	public String toaddpic(Model model){
		return "/mng/main/index/addpic";
	}
	
	@RequestMapping("/dashboard")
	public String dashboard(Model model){
		Map params = new HashMap();
		params.put("orderStatus", 2);
		params.put("transferStatus", 0);
		model.addAttribute("waitcount",memberService.getListCountByParams(params));
		return "/mng/main/dashboard";
	}
	
	@RequestMapping("/topiclist")
	public String topiclist(Model model){
		model.addAttribute("picList", indexService.getIndexPicList(null));
		return "/mng/main/index/piclist";
	}
	
	@RequestMapping("/changestat")
	public String updatepic(Integer id,Integer status,Model model){
		IndexPicInfo indexPicInfo = new IndexPicInfo();
		indexPicInfo.setId(id);
		indexPicInfo.setImgStatus(status);
		indexService.updateIndexPic(indexPicInfo);
		model.addAttribute("picList", indexService.getIndexPicList(null));
		return "/mng/main/index/piclist";
	}
	
	@RequestMapping("/delpic")
	public String deletepic(Integer id,Model model){
		indexService.deleteIndexPic(id);
		model.addAttribute("picList", indexService.getIndexPicList(null));
		return "/mng/main/index/piclist";
	}

}
